#include "Game Engine/Header/Plane.h"

Plane::Plane(const D3DXVECTOR3& origin, const D3DXVECTOR3& normal)
{
	this->normal = normal;
	this->origin = origin;
	equation[0] = normal.x;
	equation[1] = normal.y;
	equation[2] = normal.z;
	equation[3] = -(normal.x*origin.x+normal.y*origin.y
	+normal.z*origin.z);
}

// Construct from triangle:
Plane::Plane(const D3DXVECTOR3& p1,const D3DXVECTOR3& p2, const D3DXVECTOR3& p3)
{
	D3DXVec3Cross(&normal, &(p2-p1), &(p3-p1));
	D3DXVec3Normalize(&normal, &normal);
	origin = p1;
	equation[0] = normal.x;
	equation[1] = normal.y;
	equation[2] = normal.z;
	equation[3] = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
}

bool Plane::isFrontFacingTo(const D3DXVECTOR3& direction) const
{
	double dot = D3DXVec3Dot(&normal, &direction);
	return (dot <= 0);
}

double Plane::signedDistanceTo(const D3DXVECTOR3& point) const
{
	return D3DXVec3Dot(&point, &normal) + equation[3];
}